package main
{
	import api.dataStruct.ReadStoreVO;
	import api.dataStruct.SaveStoreVO;
	import api.event.CallFunName;
	
	import base.GlobalData;
	
	import com.game.api.APICore;
	import com.game.btnClass.AddMouseEvent;
	import com.game.btnClass.BtnClass_bright;
	import com.game.btnClass.BtnClass_goto;
	import com.game.data.GameStaticData;
	import com.game.display.DisManage;
	import com.game.utils.GlobalFun;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mc.GameTestBtn;
	
	public class Game extends MovieClip
	{
	
		
		
		private var _btnListName:Array=["结束游戏"]//,"购买道具","获取道具列表","使用道具","保存进度","读取进度"];
		
		private var _btnEventList:Vector.<AddMouseEvent>=new Vector.<AddMouseEvent>();
		public function Game()
		{
			init();
		}
	
		private function init():void{
			GlobalData.apiCore.currClass=this//必须添加;
			copyBtnList();
			APICore.getInstance().callAPIFun(CallFunName.A_SET_GAME_STATE,true)
		}
		private function gc():void{
			for(var i:uint=0;i<_btnEventList.length;i++){
				_btnEventList[i].delMouseEvent();
				_btnEventList[i]=null;
			}
			_btnEventList=null;
			
		}
		private function copyBtnList():void{
			for(var i:uint=0;i<_btnListName.length;i++){
				var _btn:GameTestBtn=new  GameTestBtn();
				var _point:Point=GlobalFun.getListPoint(i,7,112,40);
				_btn.x=28+_point.x;
				_btn.y=42+_point.y;
				_btn.id=i;
				_btn.txt.text=_btnListName[i];
				this.addChild(_btn);
				_btnEventList.push(new BtnClass_bright(_btn,btnClick,null,null,true));
			}
		}
		private function btnClick(e:MouseEvent):void{
			var _btn:GameTestBtn=e.currentTarget as GameTestBtn;
			switch(_btn.id){
				case 0:
					setGameOver();
					break;
				case 1:
					buyItim();
					break;
				case 2:
					getItimList();
					break;
				case 3:
					useItim();
					break;
				case 4:
					setStore();
					break;
				case 5:
					getStore();
					break;
			}
		}
		//====================================
		//API调用游戏方法
		
		//初始化游戏公共方法
		public function  UseChange(data:*=null):void{
			gc();
			new InitAPI_PVP();
		}
		//暂停游戏
		public function  stopGame(isStop:Boolean):void{
			
			if(isStop){
				stage.frameRate=GameStaticData.PAUSE_RATE;
			}else{
				stage.frameRate=GlobalData.frameRate;
			}
			
		}
		//====================================
		//游戏调用API方法
		private function buyItim():void{
			GlobalData.apiCore.callAPIFun(CallFunName.A_BUY_ITIM);
		}
		private function useItim():void{
			GlobalData.apiCore.callAPIFun(CallFunName.A_USE_ITIM);
		}
		private function getItimList():void{
			GlobalData.apiCore.callAPIFun(CallFunName.A_GET_ONE_ITIM);
		}
		private function setStore():void{
			trace("setStore");
			GlobalData.apiCore.callAPIFun(CallFunName.A_SAVE_STORE,new SaveStoreVO("战绩1",{d:1,f:2,e:3},3,1));
		}
		private function getStore():void{
			GlobalData.apiCore.callAPIFun(CallFunName.A_READ_STORE,new ReadStoreVO(3,1));
			
		}
		private function setRecord():void{
			GlobalData.apiCore.callAPIFun(CallFunName.A_SET_RECORD,GlobalFun.getRanNum(1000));
		}
		//====================================
		//游戏间通信方法
		private function setGameOver():void{
			if(GlobalData.isRobot){
				recGameOver();
			}else {
				GlobalData.apiCore.sendToGameFun("recGameOver",1,"游戏结束喽");
			}
		}
		public function recGameOver(data:*=null):void{
			setRecord();
			gc();
			DisManage.delAllDis(this);
			DisManage.delAllDis();
			DisManage.addDis(new GameOver());
		}
		private function sendTimeOver():void{
			if(GlobalData.isRobot){
				recTimeOver();
			}else{
				GlobalData.apiCore.sendToGameFun("recTimeOver",1);
			}
		}
		public  function recTimeOver(data:*=null):void{
		}
		
	}
}